<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	/**
	 * This static class is responsible for transfering messages from the cache
	 * into a permanent log if logged is enabled.
	 */
	class lib_logging {
		
		/**
		 * When a cache shrink is triggered this method is called to transfer the
		 * data from the cache to a permanent log before the data is deleted.
		 *
		 * @param string $cache_id The id of the cache file that is being shrunk
		 */
		public static function cache_event($cache_id){
			global $settings;
			
			if(strpos($cache_id,'messages/') !== false){
				
				// Check if room logging is enabled
				$rid = str_replace('messages/','',$cache_id);
				$room = new lib_room($rid);
				
				// Return if the room has logged disabled
				if(!$room->is_valid() || !$room->data_cache['log'])
					return;
				
				// Try to get the existing log data from the database
				try{
					$existing_log = $GLOBALS['DATABASE']->get('logs',$room->data_cache['rid']);
					$existing_log = $existing_log->get_next();
				}catch(dbexception $dbex){
					trigger_error("Corrupted logs database table detected",E_USER_ERROR);
				}
				
				// Make sure this data is defined
				if(empty($existing_log)){
					$existing_log['rid'] = $room->data_cache['rid'];
					$existing_log['log_text'] = '';
					$last_logged = 0;
				}else
					$last_logged = $existing_log['last_logged'];
					
				// Get the most recent record
				$rdata = $GLOBALS['CACHE']->getline($cache_id,0,true);
				
				// Immediately update the table to help prevent races
				$existing_log['last_logged'] = $rdata['lastUpdateTime'];
				$GLOBALS['DATABASE']->put('logs',$existing_log);
				
				// Add unlogged messages to the log
				$towrite = '';
				for($pos = 0; $message = $GLOBALS['CACHE']->getline($cache_id,$pos,true); $pos++){
					if($message['lastUpdateTime'] <= $last_logged) break;

					if($message['operation'] == 'new_message'){
						if(!empty($message['data']['name']))
							$message['data']['name'] .= ": ";
						
						$towrite = "[".date($settings['long_date_format'],$message['data']['time'])."] {$message['data']['name']}{$message['data']['msg']}\n".$towrite;
					}
				}
				
				$existing_log['log_text'] .= $towrite;
				
				// Write new data back to database
				$GLOBALS['DATABASE']->put('logs',$existing_log);
				
			}
		}

	}

?>